iT邦幫忙

2024 iThome 鐵人賽

DAY 4
2
Software Development

Unity使用教學系列 第 4

Unity連接Firebase_Messaging

  • 分享至 

  • xImage
  •  

如何在unity使用Firebase Messaging的功能?

  1. 在Unity Assets中create C# script (ex.FBS)
    https://ithelp.ithome.com.tw/upload/images/20240918/20169437lHXeGL52GC.png

  2. 在FBS中,寫入下方的code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Firebase.Extensions;

public class FBS : MonoBehaviour
{
    public void Start()
    {
        Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWithOnMainThread(task => {
            var dependencyStatus = task.Result;
            if (dependencyStatus == Firebase.DependencyStatus.Available) {
                // Create and hold a reference to your FirebaseApp,
                // where app is a Firebase.FirebaseApp property of your application class.
                Firebase.FirebaseApp app = Firebase.FirebaseApp.DefaultInstance;

                // Set a flag here to indicate whether Firebase is ready to use by your app.
    } else {
        UnityEngine.Debug.LogError(System.String.Format(
        "Could not resolve all Firebase dependencies: {0}", dependencyStatus));
        // Firebase Unity SDK is not safe to use here.
    }
    });

    Firebase.Messaging.FirebaseMessaging.TokenReceived += OnTokenReceived;
    Firebase.Messaging.FirebaseMessaging.MessageReceived += OnMessageReceived;

    }

    public void OnTokenReceived(object sender, Firebase.Messaging.TokenReceivedEventArgs token) {
        UnityEngine.Debug.Log("Received Registration Token: " + token.Token);
    }

    public void OnMessageReceived(object sender, Firebase.Messaging.MessageReceivedEventArgs e) {
        UnityEngine.Debug.Log("Received a new message from: " + e.Message.From);
    }

}
  1. 按左上角File -> Build setting -> Build (APK檔)
    https://ithelp.ithome.com.tw/upload/images/20240918/20169437ebR03V5u7G.png
  2. 開啟Firebase專案,按Messaging
    https://ithelp.ithome.com.tw/upload/images/20240918/20169437xEPWdR0Dsc.png
  3. 按建立第一個廣告活動
    https://ithelp.ithome.com.tw/upload/images/20240918/20169437jUTA7l2HpF.png
  4. 按firebase通知訊息(只要有安裝APP,都會收到訊息,不用打開APP)
    https://ithelp.ithome.com.tw/upload/images/20240918/20169437o7JN0URS7N.png
  5. 寫下要通知的標題和內容
    https://ithelp.ithome.com.tw/upload/images/20240918/201694372qI0wbT8Cs.png
  6. 在Unity的player setting -> player -> package name,在下方照片位置寫入
    https://ithelp.ithome.com.tw/upload/images/20240918/20169437XVsRZETWLG.png
  7. 設定想要傳送訊息的時間
    https://ithelp.ithome.com.tw/upload/images/20240918/20169437jyQIBizRMo.png
  8. 按下一步(如果沒有重要事件)
    https://ithelp.ithome.com.tw/upload/images/20240918/20169437hsoOjvjsgn.png
  9. 設定到期時間以及音效,最後按審查
    https://ithelp.ithome.com.tw/upload/images/20240918/20169437bW8QCPpicP.png
  10. 按發佈
    https://ithelp.ithome.com.tw/upload/images/20240918/20169437pCNnumzUTn.png
  11. 可以看到訊息正在傳送當中
    https://ithelp.ithome.com.tw/upload/images/20240918/20169437j2KupZLaSB.png
  12. 可以看到傳送的次數
    https://ithelp.ithome.com.tw/upload/images/20240918/20169437C10m9jpyyQ.png
  13. 在手機上收到訊息
    https://ithelp.ithome.com.tw/upload/images/20240918/20169437KKYz3fMGhG.jpg

可能遇到的問題
按發佈之後,手機沒有收到訊息,為甚麼?

  1. 網路的問題,可能要等3-5分鐘之後,就會收到了
  2. 可能package name寫錯了要回去檢查一下

上一篇
Unity連接Firebase
下一篇
Unity_使用 Firebase Realtime Database 建立會員系統
系列文
Unity使用教學15
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言